var global = 0;
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener("online", this.onOnline, false);
        document.addEventListener("offline", this.onOffline, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
    	app.ready = true;
//        console.log('Received Event: ' + id);
//        console.log('Received isMobile: ' + Global.isMobile());
        Global.loginInfo = Global.getItem(Global.loginKey);
        Global.route();
//        var c = navigator.camera;
//        for (var i in c) {
//            console.log('navigator.camera.' + i);
//        }
    },
    /**
	 * 网络连接检查
	 */
	checkConnection: function() {
		if (!app.ready) {
			return false;
		}
		var type = navigator.network.connection.type;
	    var states = {};
	    states[Connection.UNKNOWN]  = 'NO1';//'未知的网络服务器';
	    states[Connection.ETHERNET] = 'OK';//'Ethernet connection';
	    states[Connection.WIFI]     = 'OK';//'WiFi connection';
	    states[Connection.CELL_2G]  = 'OK';//'Cell 2G connection';
	    states[Connection.CELL_3G]  = 'OK';//'Cell 3G connection';
	    states[Connection.CELL_4G]  = 'OK';//'Cell 4G connection';
	    states[Connection.NONE]     = 'NO2';//'没有网络连接';
	    var type = states[type];
	    if (type == 'NO1') {
	    	Global.longToast('未知的网络服务器');
	    	return false;
	    } else if (type == 'NO2') {
	    	Global.longToast('没有网络连接');
	    	return false;
	    } else if (type == 'OK') {
	    	return true;
	    } else {
	    	Global.longToast('未知的网络');
	    	return false;
	    }
	},
	onOnline: function() {
		Global.longToast('online.');
	},
	onOffline: function() {
		Global.longToast('offline.');
	}
};

//function onPhotoDataSuccess(imageData) {
//    // Uncomment to view the base64-encoded image data
//    // console.log(imageData);
//    // Get image handle
//    var smallImage = document.getElementById('smallImage');
//    // Unhide image elements
//    //
//    smallImage.style.display = 'block';
//    // Show the captured photo
//    // The in-line CSS rules are used to resize the image
//    //
//    smallImage.src = "data:image/jpeg;base64," + imageData;
//}
//
///**
// * 打开相机
// */
//function getPhoto() {
//// Retrieve image file location from specified source
//    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50,
//    	destinationType: Camera.DestinationType.DATA_URL
//    });
//}
//
//  // Called if something bad happens.
//  //
//function onFail(message) {
//    alert('Failed because: ' + message);
//}
//
//function checkConnection() {
//	console.log("navigator.connection.type=" + navigator.connection.type);
//	alert("navigator.connection.type=" + navigator.connection.type);
//}
//
//function check_network() {
//    var networkState = navigator.network.connection.type;
//
//    var states = {};
//    states[Connection.UNKNOWN]  = 'Unknown connection';
//    states[Connection.ETHERNET] = 'Ethernet connection';
//    states[Connection.WIFI]     = 'WiFi connection';
//    states[Connection.CELL_2G]  = 'Cell 2G connection';
//    states[Connection.CELL_3G]  = 'Cell 3G connection';
//    states[Connection.CELL_4G]  = 'Cell 4G connection';
//    states[Connection.NONE]     = 'No network connection';
//
//    confirm('Connection type:\n ' + states[networkState]);
//}
//
//function testApp() {
//	for (var i in navigator) {
//		console.log('navigator.' + i + '=' + eval('navigator.' + i));
//	}
//}
//function isMobile(){
////	if (/\((iPhone|iPod|iPad|android)/i.test(navigator.userAgent)) {  
//	if (/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/i.test(navigator.userAgent)) {  
//		return true;
//	}
//	return false;
//}
